package com.syxd.controller;

import com.syxd.common.annotation.LogOperation;
import com.syxd.common.constant.Constant;
import com.syxd.common.entity.WebResult;
import com.syxd.common.utils.AssertUtils;
import com.syxd.common.utils.ExcelUtils;
import com.syxd.common.utils.PageData;
import com.syxd.dto.SysPermissionDTO;
import com.syxd.dto.SysRoleDTO;
import com.syxd.dto.SysRolePermissionDTO;
import com.syxd.entity.SysRoleEntity;
import com.syxd.entity.SysRolePermissionEntity;
import com.syxd.excel.SysRoleExcel;
import com.syxd.service.SysPermissionService;
import com.syxd.service.SysRolePermissionService;
import com.syxd.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 系统角色表
 *
 * @author hr tecsmile@outlook.com
 * @since 1.0.0 2019-07-15
 */
@RestController
@RequestMapping("sys/sysrole")
@Api(tags="系统角色表")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysPermissionService sysPermissionService;
    @Autowired
    private SysRolePermissionService sysRolePermissionService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    @LogOperation(apiType = "sysrole",apiPath = "sys/sysrole/page")
    @RequiresPermissions("sys:sysrole:page")
    public WebResult<PageData<SysRoleDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<SysRoleDTO> page = sysRoleService.page(params);

        return new WebResult<PageData<SysRoleDTO>>().ok(page);
    }

    @GetMapping("list")
    @ApiOperation("角色集合")
    @LogOperation(apiType = "sysrole",apiPath = "sys/sysrole/list")
    @RequiresPermissions("sys:sysrole:list")
    public WebResult<List<SysRoleDTO>> list(@ApiIgnore @RequestParam Map<String, Object> params){
        List<SysRoleDTO> list = sysRoleService.list(params);

        return new WebResult<List<SysRoleDTO>>().ok(list);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    @LogOperation(apiType = "sysrole",apiPath = "sys/sysrole/{id}")
    @RequiresPermissions("sys:sysrole:info")
    public WebResult<SysRoleDTO> get(@PathVariable("id") Long id){
        SysRoleDTO data = sysRoleService.get(id);

        return new WebResult<SysRoleDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation(apiType = "sysrole",apiPath = "sys/sysrole")
    @RequiresPermissions("sys:sysrole:save")
    public WebResult save(@RequestBody SysRoleDTO dto){
        //效验数据

        sysRoleService.save(dto);

        return new WebResult();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation(apiType = "sysrole",apiPath = "sys/sysrole")
    @RequiresPermissions("sys:sysrole:update")
    public WebResult update(@RequestBody SysRoleDTO dto){
        //效验数据

        sysRoleService.update(dto);

        return new WebResult();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation(apiType = "sysrole",apiPath = "sys/sysrole")
    @RequiresPermissions("sys:sysrole:delete")
    public WebResult delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        sysRoleService.delete(ids);

        return new WebResult();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation(apiType = "sysrole",apiPath = "sys/sysrole/export")
    @RequiresPermissions("sys:sysrole:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<SysRoleDTO> list = sysRoleService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, list, SysRoleExcel.class);
    }

    @GetMapping("getRolePermission")
    @ApiOperation("通过角色获取用户权限")
    @LogOperation(apiType = "sysrole",apiPath = "sys/sysrole/getRolePermission")
    @RequiresPermissions("sys:sysrole:getRolePermission")
    public WebResult getPermissionByRoleId(String roleId){
        List<SysPermissionDTO> permissionList = sysPermissionService.getByRoleId(roleId);
        return new WebResult().ok(permissionList);
    }

    @PostMapping("saveWithPermission")
    @ApiOperation("保存角色和权限信息")
    @LogOperation(apiType = "sysrole",apiPath = "/sys/sysrole/saveWithPermission")
    @RequiresPermissions("sys:sysrole:save-permission")
    public WebResult saveWithPermission(@RequestBody SysRolePermissionDTO rolePermission){
        SysRoleEntity sysRole = rolePermission.getSysRole();
        sysRole.setCreateTime(new Date());
        sysRoleService.save(sysRole);
        List<SysRolePermissionEntity> rolePermissionList = new ArrayList<>();
        for (Long permissionId : rolePermission.getSysPermissionId()) {
            rolePermissionList.add(SysRolePermissionEntity.builder().sysRoleId(sysRole.getRoleId())
                    .sysPermissionId(permissionId).isDelete(0).build());
        }
        sysRolePermissionService.insertBatch(rolePermissionList);

        return new WebResult();
    }

}
